package com.huawei.hianalytics.h;

import android.content.Context;
import com.huawei.hianalytics.e.a;
import com.huawei.hianalytics.g.b;
import java.io.Closeable;
import java.security.KeyStore;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

public class f implements X509TrustManager {
    protected List<X509TrustManager> a = new ArrayList();

    public f() {
        Context s = a.a().f().s();
        if (s == null) {
            throw new IllegalArgumentException("context is null");
        }
        Closeable closeable = null;
        try {
            TrustManagerFactory instance = TrustManagerFactory.getInstance("X509");
            KeyStore instance2 = KeyStore.getInstance("bks");
            closeable = s.getAssets().open("hianalyticscas.bks");
            closeable.reset();
            instance2.load(closeable, "".toCharArray());
            instance.init(instance2);
            for (TrustManager trustManager : instance.getTrustManagers()) {
                if (trustManager instanceof X509TrustManager) {
                    this.a.add((X509TrustManager) trustManager);
                }
            }
            if (this.a.isEmpty()) {
                throw new CertificateException("X509TrustManager is empty");
            }
        } finally {
            com.huawei.hianalytics.util.f.a(7, closeable);
        }
    }

    public void checkClientTrusted(X509Certificate[] x509CertificateArr, String str) {
        if (this.a.isEmpty()) {
            throw new CertificateException("checkClientTrusted CertificateException");
        }
        ((X509TrustManager) this.a.get(0)).checkClientTrusted(x509CertificateArr, str);
    }

    public void checkServerTrusted(X509Certificate[] x509CertificateArr, String str) {
        if (this.a.isEmpty()) {
            throw new CertificateException("checkServerTrusted CertificateException");
        }
        ((X509TrustManager) this.a.get(0)).checkServerTrusted(x509CertificateArr, str);
    }

    public X509Certificate[] getAcceptedIssuers() {
        try {
            List arrayList = new ArrayList();
            for (X509TrustManager acceptedIssuers : this.a) {
                arrayList.addAll(Arrays.asList(acceptedIssuers.getAcceptedIssuers()));
            }
            return (X509Certificate[]) arrayList.toArray(new X509Certificate[arrayList.size()]);
        } catch (Exception e) {
            b.d("SecureX509TrustManager", "getAcceptedIssuers exception : " + e.getMessage());
            return new X509Certificate[0];
        }
    }
}
